
package com.ifcar.app.adapter;

import android.content.Context;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class HeadlineAdapter extends BaseAdapter {
    
    private Context mContext;

    private List mList;

    public HeadlineAdapter(Context context, List list) {
        mContext = context;
        mList = list;
    }

    /**
     * 根据position,返回这个位置对应的比较大的中间的位置.
     * 
     * @param position
     * @return
     */
    public int getInitPosition(int position) {
        if (position < 0) {
            return position;
        }

        final int realCount = getRealCount();
        if (realCount <= 0) {
            return position;
        }

        position = getRealPosition(position);

        final int count = getCount();

        return count / 2 - (count / 2) % realCount + position;
    }

    /**
     * 返回在列表中的真实position.
     * 
     * @param position
     * @return
     */
    public int getRealPosition(int position) {
        if (position < 0) {
            return position;
        }
        final int realCount = getRealCount();
        if (realCount <= 0) {
            return position;
        }
        return position % realCount;
    }

    /**
     * 返回在列表真实count.
     * 
     * @param position
     * @return
     */
    public int getRealCount() {
        return mList.size();
    }

    @Override
    public int getCount() {
        return getRealCount() > 0 ? Integer.MAX_VALUE : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(getRealPosition(position));
    }
    
    public Context getContext() {
        return mContext;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    
}
